﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Register : System.Web.UI.Page
{
    string wjsql,sql;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {

            bind_d();
            Areabig();
        }
    }
    public void Areabig()//地区大类
    {
        sql = "select * from Area where oneid<>0 and twoid=0 and threeid=0 order by sort";
        DataTable dt = Class1.ExecSel(sql);
        Ddl_AreaOne.DataSource = dt;
        Ddl_AreaOne.DataTextField = "areaname";
        Ddl_AreaOne.DataValueField = "oneid";
        Ddl_AreaOne.DataBind();
    }
    protected void Ddl_AreaOne_SelectedIndexChanged(object sender, EventArgs e)
    {
        Ddl_AreaTwo.Items.Clear();
        Ddl_AreaTwo.Items.Add(new ListItem("全部", "0"));
        Ddl_AreaThree.Items.Clear();
        Ddl_AreaThree.Items.Add(new ListItem("全部", "0"));

        sql = "select * from Area where twoid<>0 and threeid=0 and oneid=" + Ddl_AreaOne.SelectedValue + " order by sort";
        DataTable dt = Class1.ExecSel(sql);
        Ddl_AreaTwo.DataSource = dt;
        Ddl_AreaTwo.DataTextField = "areaname";
        Ddl_AreaTwo.DataValueField = "twoid";
        Ddl_AreaTwo.DataBind();
    }
    protected void Ddl_AreaTwo_SelectedIndexChanged(object sender, EventArgs e)
    {
        Ddl_AreaThree.Items.Clear();
        Ddl_AreaThree.Items.Add(new ListItem("全部", "0"));

        sql = "select * from Area where threeid<>0 and oneid=" + Ddl_AreaOne.SelectedValue;
        sql += " and twoid=" + Ddl_AreaTwo.SelectedValue + " order by sort";
        DataTable dt = Class1.ExecSel(sql);
        Ddl_AreaThree.DataSource = dt;
        Ddl_AreaThree.DataTextField = "areaname";
        Ddl_AreaThree.DataValueField = "threeid";
        Ddl_AreaThree.DataBind();
    }

    public void bind_d()//绑定大类
    {
        sql = "select * from Class where Menu_Level=1";
        DataTable dt = Class1.ExecSel(sql);
        this.DropDownList1.DataSource = dt;
        this.DropDownList1.DataTextField = "Menu_Name";
        this.DropDownList1.DataValueField = "Menu_ID";
        this.DropDownList1.DataBind();
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string ID = this.DropDownList1.SelectedValue;

        DropDownList2.Items.Clear();
        DropDownList2.Items.Add(new ListItem("全部", "0"));
        DropDownList3.Items.Clear();
        DropDownList3.Items.Add(new ListItem("全部", "0"));

        sql = "select * from Class where Menu_Level=2 and Menu_ParentLevel=" + ID;
        DataTable dt = Class1.ExecSel(sql);
        this.DropDownList2.DataSource = dt;
        this.DropDownList2.DataTextField = "Menu_Name";
        this.DropDownList2.DataValueField = "Menu_ID";
        this.DropDownList2.DataBind();

    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string ID = this.DropDownList2.SelectedValue;

        DropDownList3.Items.Clear();
        DropDownList3.Items.Add(new ListItem("全部", "0"));

        sql = "select * from Class where Menu_Level=3 and Menu_ParentLevel=" + ID;
        DataTable dt = Class1.ExecSel(sql);
        DropDownList3.DataSource = dt;
        DropDownList3.DataValueField = "Menu_ID";
        DropDownList3.DataTextField = "Menu_Name";
        DropDownList3.DataBind();
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length >= 6)
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
    protected void Btn_Reg_Click(object sender, EventArgs e)
    {
        string UserName = TextBox1.Text.Trim();
        string Name = TextBox2.Text.Trim();
        string UserPwd = TextBox3.Text.Trim();
        string Questions = TextBox5.Text.Trim();
        string Answer = TextBox6.Text.Trim();
        string Email = TextBox7.Text.Trim();

        string Ddl1 = DropDownList1.SelectedValue;
        string Ddl2 = DropDownList2.SelectedValue;
        string Ddl3 = DropDownList3.SelectedValue;

        string Ddl4 = Ddl_AreaOne.SelectedValue;
        string Ddl5 = Ddl_AreaTwo.SelectedValue;
        string Ddl6 = Ddl_AreaThree.SelectedValue;
        string Sex = RadioButtonList1.SelectedValue;


        Md5Pw md5 = new Md5Pw();//MD5加密
        string Pwd = md5.Md5(UserPwd);
        DateTime time = DateTime.Now;
        if (Ddl1 == "0" && Ddl2 == "0" && Ddl3 == "0")
        {
            Response.Write("<script>alert('请选择分类！！');history.go(-1);</script>");
            return;
        }
        else if (Ddl4 == "0" && Ddl5 == "0" && Ddl6 == "0")
        {
            Response.Write("<script>alert('请选择城市！！');history.go(-1);</script>");
            return;
        }
        else if (this.CheckBox1.Checked)
        {
            wjsql = "select count(*) from wj_User where wj_User='" + TextBox1.Text + "'";
            int i = int.Parse(Class1.SelOne(wjsql));
            if (i == 0)
            {
                wjsql = "insert into wj_User(wj_User,wj_UserPwd,wj_Questions,wj_Answer,wj_Sex,wj_Email,wj_time,wj_Connecter,C_One,C_Two,C_Three,AreaOne,AreaTwo,AreaThree)values('" + UserName + "','" + Pwd + "','" + Questions + "','" + Answer + "','" + Sex + "','" + Email + "','" + time + "','" + Name + "'," + Ddl1 + "," + Ddl2 + "," + Ddl3 + "," + Ddl4 + "," + Ddl5 + "," + Ddl6 + ")";
                Class1.ExecSql(wjsql);
                Response.Write("<script language=javascript>alert('注册成功！');document.location='uPassport.aspx'</script>");
            }
            else
            {
                Response.Write("<script>alert('对不起，你填写的用户名已存在！');history.go(-1);</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('您未同意中国过滤布名城网服务条款！');history.go(-1);</script>");
        }
       
    }

    protected void jiance_Click(object sender, EventArgs e)
    {
        string UserName = TextBox1.Text.Trim();
        if (UserName == "")
        {
            Response.Write("<script>alert('请输入用户名查询！');history.go(-1);</script>");
        }
        else
        {
            wjsql = "select count(*) from wj_User where wj_User='" + TextBox1.Text + "'";
            int i = int.Parse(Class1.SelOne(wjsql));
            if (i == 0)
            {
                Response.Write("<script>alert('恭喜您！此帐号可以注册！');history.go(-1);</script>");
            }
            else
            {
                Response.Write("<script>alert('对不起！此帐号已存在！');history.go(-1);</script>");
            }
        }
    }
}
